<template>
  <p-drawer
    placement="right"
    :closable="false"
    width="calc(100vw - 200px)"
    :visible="visible"
    :z-index="1000"
    :mask-closable="false"
    :destroy-on-close="true"
    @close="onClose"
  >
    <TaskTitle icon-name="Det" title-name="线索详情" color="green" />
    <p-button type="primary" class="btn" @click="getDrawer">
      编辑
    </p-button>
    <div class="list-container">
      <TableLose v-if="showTable" :data-source="loseList" />
      <TitleSecondary title="基本信息" />
      <FormClueList :form="form" />
      <ProductList :data-source="detail.leadsProductList" />
      <ContactList :data-source="detail.leadsContactList" />
      <SalesTeamList :data-source="detail.leadsSalesTeamList" />
      <VisitList :data-source="detail.visitList" />
      <AttachList :data-source="detail.attachmentList" />
      <ProgressLog :list="operationLogInformation" />
      <BottomLine />
    </div>
    <ButtonGroup
      class="footer"
      :options="footerButtons"
      @click="handleButtonClick"
    />
    <DialogClueLose
      :visible.sync="visibleModal"
      :clue-id="detail.id"
      @fresh="onClose"
    />
    <DialogClueTransfer
      :visible.sync="visibleTransfer"
      :clue-id="detail.id"
      :leads-no="detail.leadsNo"
      @fresh="onClose"
    />
  </p-drawer>
</template>

<script>
import {
  getOperationLog,
  postleadstransBiz,
  postClueCreate,
  getClueFollowDetails,
} from '@/api/clue.js';
import TableLose from '@/components/table/TableLose.vue';
import DialogClueTransfer from '@/components/dialog/DialogClueTransfer.vue';
import DialogClueLose from '@/components/dialog/DialogClueLose.vue';
import FormClueList from '@/components/task/FormClueList.vue';
import ProductList from '@/components/table/ProductList.vue';
import ContactList from '@/components/table/ContactList.vue';
import SalesTeamList from '@/components/table/SalesTeamList.vue';
import VisitList from '@/components/table/VisitList.vue';
import AttachList from '@/components/table/AttachList.vue';
import ProgressLog from '@/components/task/ProgressLog.vue';
import DrawerMixin from './mixin';
export default {
  components: {
    ProductList,
    ContactList,
    SalesTeamList,
    VisitList,
    AttachList,
    ProgressLog,
    FormClueList,
    DialogClueLose,
    DialogClueTransfer,
    TableLose,
  },
  mixins: [DrawerMixin],
  data() {
    return {
      visibleModal: false,
      visibleTransfer: false,
      visibleDrawer: false,
      showTable: false,
      loseList: [],
      operationLogInformation: [], //操作日志请求返回的信息
    };
  },
  computed: {
    form() {
      return this.detail || {};
    },
    footerButtons() {
      return [
        {
          key: 'business',
          label: '转商机审批',
          type: 'primary',
          authKey: 'clue-follow-button-business',
        },
        {
          key: 'transfer',
          label: '转派',
          authKey: 'clue-follow-button-transfer',
        },
        {
          key: 'lose',
          label: '失销',
          authKey: 'clue-follow-button-lose',
        },
        {
          key: 'close',
          label: '返回',
        },
      ];
    },
  },
  watch: {
    visible: {
      handler(val) {
        val && this.handleChangeIcon();
        val && this.getLose();
        val && this.getList();
      },
      immediate: true,
    },
  },
  created() {},
  methods: {
    async getList() {
      const res = await getClueFollowDetails(this.detail.leadsNo);
      this.form = res.data?.data || [];
    },
    // 失销列表
    async getLose() {
      if (this.detail.leadsStatus === '1') {
        this.showTable = true;
        const res = await getClueFollowDetails(this.detail.leadsNo);
        this.loseList = res.data?.data?.loseList || [];
      } else {
        this.showTable = false;
      }
    },
    getDrawer() {
      this.$emit('value');
    },
    // 操作日志
    async handleChangeIcon() {
      const res = await getOperationLog('leads', this.detail.leadsNo);
      this.operationLogInformation = res.data.data;
    },
    onClose() {
      this.$emit('update:visible', false);
      this.$emit('fresh');
    },
    //转商机审批
    async businessCallback() {
      try {
        await this.$confirm('确定申请转商机审批吗？');
        console.log('1');
        const res = await postClueCreate({
          bizModule: 'leads',
          currStatus: '2',
          bizType: '2',
        });
        const processId = res.data.data;
        await postleadstransBiz({
          leadsNo: this.detail.leadsNo,
          processId: processId,
        });
        this.$message.success('转商机成功');
        this.onClose();
      } catch (error) {
        //
        // console.log('error: ', error);
      }
    },
    //转派
    transferCallback() {
      this.visibleTransfer = true;
    },
    //失销
    loseCallback() {
      this.visibleModal = true;
    },
    // 返回
    closeCallback() {
      this.onClose();
    },
    handleButtonClick(key) {
      const fn = `${key}Callback`;
      this[fn] && this[fn]();
    },
  },
};
</script>

<style lang="less" scoped>
.footer {
  height: 50px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 1px solid #e5e6e9;
}
.list-container {
  height: calc(100vh - 110px);
  overflow-y: scroll;
  position: relative;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  padding: 10px 24px;
}

/deep/ .poros-drawer-body {
  min-width: 1136px;
  height: 100%;
  padding: 0;
}
.progress-container {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.btn {
  position: absolute;
  right: 20px;
  top: 7px;
  z-index: 101;
}
</style>
